package stz.flash.main;

import net.rim.device.api.system.Display;
import stz.flash.main.flashlight.field.FontColor;
import stz.flash.main.flashlight.field.HighlightManagerForFlashMode;
import stz.flash.main.state.StrobeModeState;
import stz.flash.main.state.KeyOperator;
import stz.flash.main.state.FlashModeState;
import stz.flash.main.strobe.field.BitmapFieldForStrobeMode;
import stz.flash.main.strobe.model.VRController;
import stz.flash.main.strobe.model.VRFlashController;
import stz.flash.option.Option;
import stz.flash.option.PersistentData;
import stz.flash.option.flashlight.BacklightOption;
import stz.flash.option.flashlight.FlashlightOption;
import stz.flash.option.flashlight.LEDOption;
import stz.flash.string.OptionString;
import util.DjDoneListener;


public class ModeChangeHandler {

	private static boolean overapFilter;
	
	public static void initMode(){
		
		String selectedStartupMode = Option.getIns().getSelectedStartupMode();
		if (selectedStartupMode.equals(OptionString.OPTION_CHOICE_FIELD_MODE_LATEST)) 
			initLatestMode();
		else if (selectedStartupMode.equals(OptionString.OPTION_CHOICE_FIELD_MODE_FLASHLIGHT))
			initFlashlightMode();
		else if (selectedStartupMode.equals(OptionString.OPTION_CHOICE_FIELD_MODE_STROBE))
			initStrobeMode();
		else 
			initFlashlightMode(); 
	}
	private static void initLatestMode() {
		if (isLatestFlashlightMode()) {
			initFlashlightMode();
		} else {
			initStrobeMode();
		}
	}
	private static void initFlashlightMode() {
		AniEffect.setInt(0);
		KeyOperator.setState(FlashModeState.getIns());
		setFlashlightModeFocusable();
	}
	private static void initStrobeMode() {
		AniEffect.setInt(-Display.getWidth());
		KeyOperator.setState(StrobeModeState.getIns());
		setStrobeModeFocusable();
	}
	
	public static void loadStartupMode() {
		String selectedStartupMode = Option.getIns().getSelectedStartupMode();
		if (selectedStartupMode.equals(OptionString.OPTION_CHOICE_FIELD_MODE_LATEST)) { 
			loadLatestMode();
		} else if (selectedStartupMode.equals(OptionString.OPTION_CHOICE_FIELD_MODE_FLASHLIGHT)) {
			loadFlashlightMode();
		} else if (selectedStartupMode.equals(OptionString.OPTION_CHOICE_FIELD_MODE_STROBE)) {
			loadStrobeMode();
		} else
			loadFlashlightMode();
	}
	private static void loadFlashlightMode() {
		AniEffect.setInt(0);
		prepareFlashlightMode();
		KeyOperator.setState(FlashModeState.getIns());
	}
	private static void loadStrobeMode() {
		
		AniEffect.setInt(-Display.getWidth());
		prepareStrobeMode(new DjDoneListener() {
			
			public void done() {
				KeyOperator.setState(StrobeModeState.getIns());
			}
		});
	}
	
	public static void loadLatestMode() {
	
		if (isLatestFlashlightMode()) {
			prepareFlashlightMode();
	
		} else {
			
			prepareStrobeMode(new DjDoneListener() {
				public void done() {
					
					KeyOperator.setState(StrobeModeState.getIns());
				}
			});
		}
	}
	
	private static void notifyDoneListener(DjDoneListener doneListener) {
		if (doneListener != null)
			doneListener.done();
	}

	
	public static void changeIntoStrobeMode() {
		if (overapFilter == true)  
			return;
		else 
			overapFilter = true;
		
		new Thread() {
			public void run() {
				
				//H263의 부작용이 없어지길 기다림
				stopTimer(new DjDoneListener() {
					public void done() {
					
						prepareStrobeMode(new DjDoneListener() {
							public void done() {
								
								begineAnimationIntoStrobe();
								overapFilter = false;
							}
						});
					}
				});
			}
		}.start();
	}
	private static void stopTimer(DjDoneListener doneListener) {
	
		KeyOperator.setNextState(StrobeModeState.getIns());
		FlashTimer.getIns().stop(FlashTimer.STOP, doneListener);
	}
	private static void begineAnimationIntoStrobe(){
	
		AniEffect.nextDirection(-1);
		AniEffect.begin(0);
	}
	
	
	private static void prepareStrobeMode(final DjDoneListener doneListener) {
	
		setStateOfStrobe();
		TitleManager.disableIntoStrobeManager();
		
		VRController.runVideoRecorder(new DjDoneListener() {

			public void done() {
				applyVRFlashStateOfOption();//pushScreen도 됨
				startTimerIntoStrobeMode();
				setStrobeModeFocusable();
				setFontColorForStrobe();
				notifyDoneListener(doneListener);
			}
		});
	}	
	private static void setStateOfStrobe(){
		KeyOperator.setNextState(StrobeModeState.getIns());
		BulbField.getIns().changeStateAndUpdate(BulbStateOfStrobe.getIns());
	}
	private static void startTimerIntoStrobeMode() {
		FlashTimer.getIns().stopAndStart(FlashTimer.STOP_AND_START_into_strobe_mode);
	}
	private static void setStrobeModeFocusable(){
		HighlightManagerForFlashMode.setFocusable(false);
		BitmapFieldForStrobeMode.setFocusable(true);
	}
	private static void applyVRFlashStateOfOption() {
		VRFlashController.applyVRFlashStateOfOption();
	}
	private static void setFontColorForStrobe(){
		FontColor.setFontColor(FontColor.COLOR_BRIGHT);
	}

	
	public static void changeIntoFlashlightMode() {
		
		
		VRController.closeVR(new DjDoneListener() {

			public void done() {
				prepareFlashlightMode();
				BegineAnimationIntoFlashlight();
			}
		});
	}
	private static void BegineAnimationIntoFlashlight(){
		AniEffect.nextDirection(1);
		AniEffect.begin(0);
	}
	
	
	private static void prepareFlashlightMode() {
		TitleManager.disableIntoFlashManager();
		setStateOfFlashlight();
		updateLatestWithStartupSet();
		startTimerIntoFlashlightMode();
		pushScreen();
		setFlashlightModeFocusable();
		setFontColorForFlashlight();
		
		//test
		PersistentData.getPersistentDatabase().setTestActivity(true);
	}
	private static void setStateOfFlashlight(){
		KeyOperator.setNextState(FlashModeState.getIns());
		BulbField.getIns().changeStateAndUpdate(BulbStateOfFlashlight.getIns());

	}
	private static void updateLatestWithStartupSet() {
		FlashlightOption.getIns().updateFlashStateWithStartupSet();
		BacklightOption.getIns().updateLatestBacklightStateWithStartupSet();
		LEDOption.getIns().updateLatestLedStateWithStartupSet();
	}
	private static void startTimerIntoFlashlightMode() {
		FlashTimer.getIns().stopAndStart(FlashTimer.STOP_AND_START_into_flashlight_mode);
	}
	private static void pushScreen() {
		ScreenHandle.getIns().pushScreen();
	}
	private static void setFlashlightModeFocusable(){
		HighlightManagerForFlashMode.setFocusable(true);
		BitmapFieldForStrobeMode.setFocusable(false);
	}
	private static void setFontColorForFlashlight(){
		if (BacklightOption.getIns().isLatestBacklightState() == true)
			FontColor.setFontColor(FontColor.COLOR_DARK);
		else
			FontColor.setFontColor(FontColor.COLOR_BRIGHT);
	}

	public static boolean isLatestFlashlightMode() {
		return Option.getIns().getLatestMode() == Option.FLASHLIGHT_MODE;
	}
}